package util;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import anotation.ActionAnotation;
import anotation.ForwardAnotation;

import entity.AnotationBean;


public class AnotationParser {
	
	public static Map<String, AnotationBean> anotationParse(String serverPath, String packagePath) throws Exception {
		
        Map<String, AnotationBean> map = new HashMap<String,AnotationBean>();
        
        String[] fileNames = new File(serverPath + "WEB-INF\\classes\\" + packagePath).list();
		
		for(String fileName : fileNames) {
			
			String actionClass = packagePath + "." + fileName.split("\\.")[0];

			Class<?> clazz = Class.forName(actionClass);
			
			if(clazz.isAnnotationPresent(ActionAnotation.class)) {
				
				AnotationBean anotationBean = new AnotationBean();
				Map<String,String> forwardMap = new HashMap<String,String>();
				
				ActionAnotation actionAnotation = clazz.getAnnotation(ActionAnotation.class);
				
				anotationBean.setBeanClass(actionAnotation.beanClass());
				anotationBean.setBeanName(actionAnotation.beanName());
				anotationBean.setActionPath(actionAnotation.actionPath());
				anotationBean.setActionClass(actionClass);
				
				ForwardAnotation[] forwardAnotations = actionAnotation.forwards();
				for(ForwardAnotation forwardAnotation : forwardAnotations) {
					
					forwardMap.put(forwardAnotation.fName(), forwardAnotation.fValue());
				}
				
				anotationBean.setActionForward(forwardMap);

				map.put(actionAnotation.actionPath(), anotationBean);
				
			}
		}
		
		return map;
	}

}
